﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Microsoft.AspNet.Identity;
using Sharp.DataTransferObject;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

#endregion

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [DirectController]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var vm = new IndexModel
            {
                UserName = User.Identity.Name
            };

            vm.LeftPanel = new IndexModel.Left();

            var builder = new TreeBuilder<Guid, PermissionDTO>
            {
                ChangeData = (p, treeNode) =>
                {
                    treeNode.Text = p.Title;
                    treeNode.CustomAttributes.Add(new ConfigItem("route", p.Route.IsNullOrEmpty() ? "#" : "/" + p.Route));
                    treeNode.NodeID = p.Id.ToString();
                    treeNode.Expanded = true;
                }
            };

            var list = User.Identity.GetPermissions().Where(x => x.Category == "01")
                .GroupBy(x => x.SystemCode)
                .ToDictionary(x => x.Key, g => g.OrderBy(x => x.OrderIndex).ToList());

            List<SubSystemDTO> subSystems;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                subSystems = proxy.Channel.GetSubSystems().ToList();
            }
            if (!subSystems.Any())
            {
                subSystems = list.Select(x => new SubSystemDTO()
                {
                    Name = x.Key,
                    Code = x.Key
                }).ToList();
            }

            subSystems.AddRange(list.Where(x => subSystems.All(y => y.Code != x.Key)).Select(x => new SubSystemDTO()
            {
                Name = x.Key,
                Code = x.Key
            }));

            foreach (var system in subSystems)
            {
                vm.LeftPanel.LeftTrees.Add(system.Code, new Node()
                {
                    Text = ".",
                    NodeID = system.Code + "Root",
                    Expanded = true,
                    AutoDataBind = false,
                    Checked = null
                });

                if (list.TryGetValue(system.Code, out var thisSystem))
                {
                    builder.Build(thisSystem, vm.LeftPanel.LeftTrees.Last().Value, null);

                    vm.LeftPanel.Systems.Add(system);
                }
            }

            return View(vm);
        }


        [AutomaticTemplate()]
        public ActionResult Custom()
        {
            return new PartialViewResult()
            {
                Model = new UserPortalConfig()
            };
        }
        public ActionResult About()
        {
            return new PartialViewResult();
        }


        public ActionResult LoadSubscribers()
        {
            return new PartialViewResult
            {
                ViewName = "test",
                //ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

    }
}